<?php

namespace hotel\common\utils;

use hotel\common\models\App;
use Yii;
use yii\log\FileTarget;

class MemberServices {

  //核销优惠券
  public static function verify($memberId,$couponid,$couponname,$orderId,$appid){
    $app_data = App::find()->where("appid = '$appid'")->asArray()->one();
    $url = $app_data['coupon_url']. "verify.json";
    $key = $app_data['coupon_key'];
    $channel = $app_data['coupon_channel'];
    $sign_str = $key.date("Y").date("m");
    $sign = md5($sign_str);
    $data = [
        'channel' => $channel,
        'memberId' => $memberId,
        'couponid' => $couponid,
        'couponname' => $couponname,
        'orderId' => $orderId,
        'sign' => $sign
    ];
    $result = self::curlPost($url, $data);
    $res = json_decode($result, true);
    if($res['code'] == 00 && $res['message'] == 'ok'){
      return true;
    }
    return false;
  }

  //撤销优惠券
  public static function cancel($memberId,$couponid,$couponname,$orderId,$appid){
    $app_data = App::find()->where("appid = '$appid'")->asArray()->one();
    $url = $app_data['coupon_url']. "cancel.json";
    $key = $app_data['coupon_key'];
    $channel = $app_data['coupon_channel'];
    $sign_str = $key.date("Y").date("m");
    $sign = md5($sign_str);
    $data = [
        'channel' => $channel,
        'memberId' => $memberId,
        'couponid' => $couponid,
        'couponname' => $couponname,
        'orderId' => $orderId,
        'sign' => $sign
    ];
    $result = self::curlPost($url, $data);
    $res = json_decode($result, true);
    if($res['code'] == 00 && $res['message'] == 'ok'){
      return true;
    }
    return false;
  }


  //订单推送
  public static function orderLog($memberId,$couponid,$couponname,$orderId,$paysum,$payamount,$appid){
    $app_data = App::find()->where("appid = '$appid'")->asArray()->one();
    $url = $app_data['coupon_url']. "orderLog.json";
    $key = $app_data['coupon_key'];
    $channel = $app_data['coupon_channel'];
    $sign_str = $key.date("Y").date("m");
    $sign = md5($sign_str);
    $data = [
        'channel' => $channel,
        'memberId' => $memberId,
        'openId' => $memberId,
        'couponid' => $couponid,
        'couponname' => $couponname,
        'orderId' => $orderId,
        'billDate' => date("Y-m-d H:i:s"),
        'paysum' => $paysum,
        'payamount' => $payamount,
        'sign' => $sign
    ];

    $result = self::curlPost($url, $data);
    $res = json_decode($result, true);
    if($res['code'] == 00 && $res['message'] == 'ok'){
      return true;
    }
    return false;
  }

  public static function recordPay($options)
  {
    $log = new FileTarget();
    $log->logFile = \Yii::$app->getRuntimePath() . '/logs/coupon' . date('Ymd') . '.log';
    $log->messages[] = [
        json_encode($options)
    ];
    $log->export();
  }

  public static function curlPost($url, $param, $method = 'post', $extra = [])
  {
    $calculate_time1 = microtime(true);
    $headers = $extra['headers'];
    // 初始华
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CERTINFO, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($curl, CURLOPT_VERBOSE, true); //打印日志

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);//函数中加入下面这条语句

    if (isset(\Yii::$app->params['curl']) && isset(\Yii::$app->params['curl']['timeout'])) {
      curl_setopt($curl, CURLOPT_TIMEOUT, \Yii::$app->params['curl']['timeout']);
    } else {
      curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    }

    if (array_key_exists("HTTP_USER_AGENT", $_SERVER)) {
      curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    }

    if (!empty($headers)) {
      $headerArr = [];
      foreach ($headers as $n => $v) {
        $headerArr[] = $n . ': ' . $v;
      }
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);  //构造IP
    }

//    if ($cookie) {
//      curl_setopt($curl, CURLOPT_COOKIE, $cookie);
//    }


    // post处理
    if ($method == 'post') {
      curl_setopt($curl, CURLOPT_POST, true);
      if (is_array($param)) {
        $param = http_build_query($param);
      }

      curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
    } else {
      curl_setopt($curl, CURLOPT_POST, false);
    }

    // 执行输出
    $info = curl_exec($curl);

    //log
    $_errno = curl_errno($curl);
    $_error = '';
    if ($_errno) {
      $_error = curl_error($curl);
    }
    curl_close($curl);
    $calculate_time_span = microtime(true) - $calculate_time1;
    $log = \Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'curl.log';
    file_put_contents($log, date('Y-m-d H:i:s') . " [ time:{$calculate_time_span} ] url: {$url} \nmethod: {$method} \ndata: " . json_encode($param) . " \nresult: {$info} \nerrorno: {$_errno} error: {$_error} \n", FILE_APPEND);

    if ($_error) {
      return $_error;
    }

    return $info;
  }

}